package com.prj.ufdm.cloud.ms.auth.api;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.prj.ufdm.cloud.base.dto.RetData;
import com.prj.ufdm.cloud.base.exception.BaseException;
import com.prj.ufdm.cloud.base.util.BaseUuidUtil;
import com.prj.ufdm.cloud.ms.auth.enums.AuthRetCodeEnum;
import com.prj.ufdm.cloud.ms.core.bpo.code.SysCodeBPO;
import com.prj.ufdm.cloud.ms.core.bpo.code.SysCodeItemBPO;
import com.prj.ufdm.cloud.ms.core.model.PageBean;
import com.prj.ufdm.cloud.ms.core.model.code.SysCode;
import com.prj.ufdm.cloud.ms.core.model.code.SysCodeVO;



/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhengzixiang
 * @since 2019-09-20
 */
@RestController
@RequestMapping("/api/auth/sysCode")
public class SysCodeController {

 	@Autowired
    private SysCodeBPO sysCodeBPO;
 	@Autowired
    private SysCodeItemBPO sysCodeItemBPO;
    
    /**
   	 *查询列表
    */
    @RequestMapping("/doGetList")
	public RetData<PageInfo<SysCode>> doGetList(PageBean pageBean,SysCode t)  throws Exception {
		RetData<PageInfo<SysCode>> retData = new RetData<PageInfo<SysCode>>();
		PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
		List<SysCode>  list=sysCodeBPO.doGetList(t);
		PageInfo<SysCode> page=new PageInfo<SysCode>(list);
		retData.setRetBody(page);
		return retData;
	}
    /**
   	 *查询列表
    */
    @RequestMapping("/doGetAll")
	public RetData<List<SysCode>> doGetAll(SysCode t)  throws Exception {
		RetData<List<SysCode>> retData = new RetData<List<SysCode>>();
		List<SysCode>  list=sysCodeBPO.doGetList(t);
		retData.setRetBody(list);
		return retData;
	}
	 /**
   	 *通过id查询
    */
	@RequestMapping("/doGetById")
	public RetData<SysCode> doGetById(String id)  throws Exception {
		RetData<SysCode> retData = new RetData<SysCode>();
		SysCode result=sysCodeBPO.doGetById(id);
		retData.setRetBody(result);
		return retData;
	}
	/**
   	 *保存/更新
    */
	@RequestMapping("/doSave")
	public RetData<String> doSave(SysCode t)throws Exception{
		RetData<String> ret=new RetData<String>();
		if(StringUtils.isBlank(t.getCode())) {
			throw new BaseException(AuthRetCodeEnum.RET_CODE_0104001.getCode(), AuthRetCodeEnum.RET_CODE_0104001.getMessage());
		}
		if(StringUtils.isBlank(t.getCodeName())) {
			throw new BaseException(AuthRetCodeEnum.RET_CODE_0104002.getCode(), AuthRetCodeEnum.RET_CODE_0104002.getMessage());
		}
		SysCode obj=new SysCode();
		obj.setCode(t.getCode());
		Integer total=0;
		
		if(StringUtils.isBlank(t.getId())) {
			total=sysCodeBPO.doGetTotal(obj);
			if(null!=total&&total.compareTo(0)>0) {
				throw new BaseException(AuthRetCodeEnum.RET_CODE_0104008.getCode(), "参数类型关键字："+t.getCode()+"已存在");
			}
			t.setId(BaseUuidUtil.getUUID());
			sysCodeBPO.doSave(t);
		}else {
			obj.setId(t.getId());
			total=sysCodeBPO.doGetNotMeTotal(obj);//查找code是否已被其他记录引用
			if(null!=total&&total.compareTo(0)>0) {
				throw new BaseException(AuthRetCodeEnum.RET_CODE_0104007.getCode(), "参数类型关键字："+t.getCode()+"已存在");
			}
			sysCodeBPO.doModById(t);
		}
		
		ret.setRetBody("");
		return ret;
	}
	
	/**
   	 *更新
    */
	@RequestMapping("/doModById")
	public RetData<String> doModById(SysCode t)throws Exception{
		RetData<String> ret=new RetData<String>();
		sysCodeBPO.doModById(t);
		ret.setRetBody("");
		return ret;
	}
	
	/**
   	 *删除
    */
	@RequestMapping("/doRmById")
	public RetData<String> doRmById(String id)throws Exception{
		RetData<String> ret=new RetData<String>();
		sysCodeBPO.doRmById(id);
		ret.setRetBody("");
		return ret;
	}
	/**
   	 *批量删除
    */
	@RequestMapping("/doRmByIds")
	public RetData<String> doRmByIds(@RequestParam("ids")String ids)throws Exception{
		if(StringUtils.isBlank(ids)) {
			throw new BaseException("参数不能为空");
		}
		Integer total=sysCodeItemBPO.doGetTotalBysysCodeIds(ids);
		if(null!=total&&total.compareTo(0)>0) {
			throw new BaseException(AuthRetCodeEnum.RET_CODE_0104009.getCode(),AuthRetCodeEnum.RET_CODE_0104009.getMessage() );
		}
		RetData<String> ret=new RetData<String>();
		sysCodeBPO.doRmByIds(ids);
		ret.setRetBody("");
		return ret;
	}
	
	/**
   	 *查询枚举类型列表
    */
    @RequestMapping("/doGetEnumTypeList")
	public RetData<List<SysCodeVO>> doGetEnumTypeList(String codeType, String codeEnabledState )  throws Exception {
		RetData<List<SysCodeVO>> retData = new RetData<List<SysCodeVO>>();
		List<SysCodeVO>  list=sysCodeBPO.doGetEnumType(codeType, codeEnabledState);
		retData.setRetBody(list);
		return retData;
	}
}
